import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/view/ListSeparator.dart';
import 'package:im_app/im/page/view/component/GroupIconView.dart';
import 'package:im_app/im/page/view/component/SessionIconView.dart';
import 'package:im_app/im/util/ImConstant.dart';

import '../../pojo/ImPojo.dart';
import '../../pojo/UiPojo.dart';
import '../../service/ImMessageSendService.dart';
import '../../util/MessageUtil.dart';
import '../../util/UiUtil.dart';
import '../bloc/TransferMessageCubit.dart';
import '../sub/TransferMessagePage.dart';
import 'component/UserIconView.dart';
import 'component/pane/MessagePane.dart';

// 转发消息确认页面
class TransferMessageView extends StatelessWidget {
  final List<ImMessage> messages; // 支持批量转发消息
  final List<ImSession>? sessions; // 支持想多个聊天会话转发消息
  final ImSession? session; // 转发单个聊天会话
  final SearchResult? searchResult; // 查询选项
  final SelectType selectType;
  final TransferMessageCubit transferMessageCubit;
  final bool isCreateSession; // 是否创建新的聊天

  const TransferMessageView({required this.messages, this.sessions, this.session, this.searchResult,
    required this.selectType, required this.transferMessageCubit,required this.isCreateSession, super.key});

  @override
  Widget build(BuildContext context) {
    return _buildBody(context);
  }

  Widget _buildBody(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.start,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildTitle(context),
        const SizedBox(height: 10),

        // 转发用户
        _buildUserInfo(context),
        const SizedBox(height: 10),

        // 转发信息
        Expanded(child: _buildMessagePane(context)),
        const SizedBox(height: 10),

        // 留言
        _buildLeaveMessagePane(context),
        const SizedBox(height: 10),

        ListSeparator(
          indent: 10,
        ),

        // 提交面板
        _buildSubmitPane(context),
      ],
    );
  }

  Widget _buildTitle(BuildContext context) {
    return const Padding(
      padding: EdgeInsets.only(top: 5, bottom: 5),
      child: Text(
        '发送给：',
        style: TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),
      ),
    );
  }

  Widget _buildUserInfo(BuildContext context) {
    var children = <Widget>[];
    if (searchResult != null) {
      // 搜索
      if (searchResult!.groupCode != null) {
        // 搜索选择了一个群
        children.add(GroupIconView(
          group: searchResult!.group!,
          iconSize: 30,
        ));
        children.add(Padding(
          padding: const EdgeInsets.only(left: 5),
          child: Text(
            searchResult!.group!.name ?? '',
            style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),
          ),
        ));
      } else {
        // 搜索选择了一个用户
        children.add(UserIconView(user: searchResult!.user!));
        children.add(Padding(
          padding: const EdgeInsets.only(left: 5),
          child: Text(
            searchResult!.user!.userName ?? '',
            style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),
          ),
        ));
      }
    } else if (selectType == SelectType.simple && session != null) {
      // 选择一个聊天会话
      children.add(SessionIconView(session: session!));
      children.add(Padding(
        padding: const EdgeInsets.only(left: 5),
        child: Text(
          session!.name ?? '',
          style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),
        ),
      ));
    } else if (selectType == SelectType.multiple && sessions != null) {
      // 选择多个会话
      for (var session in sessions!) {
        children.add(SessionIconView(session: session));
      }
    } else {
      throw Exception("不应该出现这种场景");
    }

    return Row(
      children: children,
    );
  }

  // 消息展示面板
  Widget _buildMessagePane(BuildContext context) {
    var transferType = transferMessageCubit.state.transferType;
    Size scrSize = MediaQuery.of(context).size;
    double screenWidth = scrSize.width * 0.85;
    if (transferType == TransferType.simple) {
      return MessageRefPane(message: messages[0], param: MessageRefParam(maxLines: 100, refMaxLength: screenWidth),);
    } else if (transferType == TransferType.oneByeOne) {
      return Text('【逐条转发消息】，共${transferMessageCubit.state.messages.length}条消息');
    } else if (transferType == TransferType.mergeOne) {
      return Text('【合并转发消息】，共${transferMessageCubit.state.messages.length}条消息');
    } else {
      return Text('不应该出现');
    }
  }

  // 留言面板
  Widget _buildLeaveMessagePane(BuildContext context) {
    return  Padding(
      padding: const EdgeInsets.only(left: 10, right: 10),
      child: TextField(
        decoration: const InputDecoration(
          border: OutlineInputBorder(gapPadding: 1, borderRadius: BorderRadius.all(Radius.circular(5))),
          fillColor: Colors.white,
          isCollapsed: true,
          filled: true,
          contentPadding: const EdgeInsets.only(top: 10, right: 5, bottom: 10, left: 5),
          hintText: '请留言',
          hintStyle: TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),
        ),
        onChanged: (String value) {
           transferMessageCubit.add(TransferMessageLeaveMessageChangeEvent(value));
        },
      ),
    );
  }

  Widget _buildSubmitPane(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(top: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          TextButton(onPressed: () {
            transferMessageCubit.add(TransferMessageCancelTransferEvent(context, isCreateSession));
            // Navigator.of(context).pop();
          }, child: const Text('取消')),
          const Divider(),
          TextButton(onPressed: () {
            // _save(context);
            transferMessageCubit.add(TransferMessageSubmitEvent(context, isCreateSession));
          }, child: const Text('确认')),
        ],
      ),
    );
  }
}
